package com.fanbuchi.app.ins.manager;

import com.fanbuchi.app.ins.BuildConfig;
import com.fanbuchi.app.ins.net.BasicAuthConfig;
import com.fanbuchi.app.ins.net.SSLConfig;
import com.squareup.okhttp.Authenticator;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;
import com.squareup.okhttp.Route;
import com.squareup.okhttp.logging.HttpLoggingInterceptor;
import com.squareup.okhttp.ws.WebSocketCall;

import java.io.IOException;
import java.net.Proxy;
import java.util.concurrent.TimeUnit;

/**
 * Created by rabbit on 2016/3/16.
 */
public class OkHttpManager {

   // -keep class com.xxx.model.xxx.** { *; }

   /*mOkHttpClient.networkInterceptors()
           .add(new CookiesInterceptor(context));*/
/* MarvelSigningInterceptor signingInterceptor = new MarvelSigningInterceptor(
         BuildConfig.MARVEL_PUBLIC_KEY, BuildConfig.MARVEL_PRIVATE_KEY);*/
       /* okhttp3 OkHttpClient client = new OkHttpClient.Builder()
                .addInterceptor(signingInterceptor)
                .addInterceptor(loggingInterceptor)
                .build();*/

   private static final OkHttpClient mOkHttpClient = new OkHttpClient();

   public static OkHttpClient getmOkHttpClient() {
      sslConfig();
      authConfig();
      logConfig();
      commonConfig();
      Authenticator mAuthenticator = new Authenticator() {
         @Override
         public Request authenticate(Proxy proxy, Response response) throws IOException {
            return null;
         }

         @Override
         public Request authenticateProxy(Proxy proxy, Response response) throws IOException {
            return null;
         }
      };
      mOkHttpClient.setAuthenticator(mAuthenticator);
      return mOkHttpClient;
   }

   protected static void commonConfig(){
      mOkHttpClient.setConnectTimeout(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
      mOkHttpClient.setReadTimeout(Integer.MAX_VALUE, TimeUnit.MILLISECONDS);
   }

   protected static void logConfig(){
      if (BuildConfig.DEBUG){
         HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
         // set your desired log level
         logging.setLevel(HttpLoggingInterceptor.Level.BODY);
         // add your other interceptors …
         // add logging as last interceptor
         OkHttpManager.mOkHttpClient.interceptors().add(logging);
         //WebSocketCall.create()
      }
   }

   protected  static void sslConfig(){
      SSLConfig.sslconfig(OkHttpManager.mOkHttpClient);
   }

   protected  static void authConfig(){
      BasicAuthConfig.basicAuthConfig(OkHttpManager.mOkHttpClient);
   }
}
